package com.woniuxy.controllers;


import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.entitys.Service;
import com.woniuxy.services.ServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "服务创建接口")
public class ServiceCreateController {
    @Autowired
    private ServiceService serviceService;

    @ApiOperation(
            value = "查询需要编辑的所有服务信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页显示的条目数",required=true),
            @ApiImplicitParam(paramType="query",name="key",value="查询条件",required=false),
            @ApiImplicitParam(paramType="query",name="value",value="查询输入内容",required=false),
    })
    @GetMapping("/list")
    //查询需要编辑的服务
    public RestPOJO selectAllService(Integer currentPage, Integer pageSize, String key, String value) {
        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Service> pageInfo = serviceService.selectAllService(currentPage, pageSize, key, value);
        if (pageInfo != null && pageInfo.getList().size() > 0) {
            restPOJO.setData(pageInfo);
            restPOJO.setStatus(1000);
            restPOJO.setMessage("查询成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("查询失败");
        }
        return restPOJO;
    }

    //查询归档服务

    @ApiOperation(
            value = "查询已经归档的所有服务信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页显示的条目数",required=true),
    })
    @GetMapping("/selall")
    public RestPOJO selectAll(Integer currentPage, Integer pageSize) {
        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Service> pageInfo = serviceService.selectAll(currentPage, pageSize);
        if (pageInfo != null && pageInfo.getList().size() > 0) {
            restPOJO.setData(pageInfo);
            restPOJO.setStatus(1000);
            restPOJO.setMessage("查询成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("查询失败");
        }
        return restPOJO;
    }
    //新建服务

    @ApiOperation(
            value = "创建新服务",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping( "/save")
    public RestPOJO saveService(@RequestBody Service service) {
        RestPOJO restPOJO = new RestPOJO();
        //service.setCom_id(Integer.parseInt(service.getCom_id()));
        int i = serviceService.saveService(service);
        if (i > 0) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("添加成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("添加失败");
        }
        return restPOJO;
    }
    }

